/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package promogui;
import java.awt.Color;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;

import java.util.logging.Level;
import java.util.logging.Logger;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PiePlot3D;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.category.BarRenderer;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.chart.urls.StandardCategoryURLGenerator;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
import org.jfree.util.Rotation;


/**
 *
 * @author Friedemann Metzger
 */
public class ChartGenerator {
    
    
    
    
    
    
    
     
    public static JFreeChart createLineChart(XYDataset dataset, String title, String unit) 
    {
       
        JFreeChart chart = ChartFactory.createXYLineChart(
            title,      // chart title
            "Wochen",                      // x axis label
            unit,                      // y axis label
            dataset,                  // data
            PlotOrientation.VERTICAL,
            true,                     // include legend
            true,                     // tooltips
            false                     // urls
        );

        /*
        PiePlot3D plot = (PiePlot3D) chart.getPlot();
        plot.setStartAngle(290);
        plot.setDirection(Rotation.CLOCKWISE);
        plot.setForegroundAlpha(0.5f);
        */
        
        
         chart.setBackgroundPaint(Color.white);

//        final StandardLegend legend = (StandardLegend) chart.getLegend();
  //      legend.setDisplaySeriesShapes(true);
        
        // get a reference to the plot for further customisation...
        final XYPlot plot = chart.getXYPlot();
        plot.setBackgroundPaint(Color.lightGray);
    //    plot.setAxisOffset(new Spacer(Spacer.ABSOLUTE, 5.0, 5.0, 5.0, 5.0));
        plot.setDomainGridlinePaint(Color.white);
        plot.setRangeGridlinePaint(Color.white);
        
        final XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer();
        renderer.setSeriesLinesVisible(0, false);
        renderer.setSeriesShapesVisible(1, false);
      //  plot.setRenderer(renderer);

        // change the auto tick unit selection to integer units only...
        final NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
        rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
        // OPTIONAL CUSTOMISATION COMPLETED.
        
        
        
        
        return chart;
        
    }
    
    
    
    
    
  
    
    
    
    
    
    
    
    
    
     public static JFreeChart createBarChart(CategoryDataset dataset, String title) 
    {
       
        JFreeChart chart = ChartFactory.createBarChart3D(
        	title, 
        	"",
        	"",
        	dataset, 
        	PlotOrientation.HORIZONTAL, 
            false,                   // include legend
            true,
            false
        );

        /*
        PiePlot3D plot = (PiePlot3D) chart.getPlot();
        plot.setStartAngle(290);
        plot.setDirection(Rotation.CLOCKWISE);
        plot.setForegroundAlpha(0.5f);
        */
        
        return chart;
        
    }
    
     
     public static DefaultCategoryDataset getBarChartDataset(HealthCriteria criteria, ArrayList<Project> project)
     {
         DefaultCategoryDataset dataset = new DefaultCategoryDataset();
         ArrayList<Double> values = criteria.calcHealthOverviewValues(project);
         Iterator<Double> it = values.iterator();
         
         int counter = 0;
         while (it.hasNext())
         {
             dataset.addValue(it.next(), "blah", project.get(counter).getName());
             counter++;
         }
     
     
     return dataset;
     }
     
     
     
     
     
 public static ChartPanel getPieOverview(Project project)
    {
       
           
        return getPieChart(project);
    
      
    
    }    
    
     public static ChartPanel getWorkedChart(Project project) 
    {
    	XYSeriesCollection dataset = new XYSeriesCollection();
        
            //float value = project.getWorkedPercent();
            String name = project.getName();

		ArrayList<Float> workedHoursPerWeek = project.getEvolutionalWorked();
                ArrayList<Float> estimatedHoursPerWeek = project.getEvolutionalEstimated();
		

                dataset.addSeries(getSeriesFloat(workedHoursPerWeek, "Ist"));
                dataset.addSeries(getSeriesFloat(estimatedHoursPerWeek, "Soll"));
                JFreeChart chart = createLineChart(dataset, "Projektverlauf " + project.getName(), "Stunden");

		return new ChartPanel(chart);
        
    }
     
     public static ChartPanel getCriteriaChart(HealthCriteria criteria, Project project)
     {
         ArrayList<Double> values = criteria.calc(project);
         XYSeriesCollection dataset = new XYSeriesCollection();
         String name = criteria.getName()+"-"+project.getName();

         dataset.addSeries(getSeriesDouble(values, ""));
         
        JFreeChart chart = createLineChart(dataset, name, "");
        chart.removeLegend();
        
        return new ChartPanel(chart);
        
     }
     
     
     public static ChartPanel getCriteriaProjectOverviewChart(HealthCriteria criteria, ArrayList<Project> project)
     {

        DefaultCategoryDataset dataset = getBarChartDataset(criteria, project);
        ArrayList<Boolean> list = criteria.isFitOverview(project);
        
        
        
            JFreeChart chart = ChartFactory.createBarChart3D(
        	criteria.getName(), 
        	"",
        	"",
        	dataset, 
        	PlotOrientation.HORIZONTAL, 
            false,                   // include legend
            true,
            true
        ); 
        
        

        
        final CategoryPlot plot = chart.getCategoryPlot();
        
        plot.setRenderer(new ProjectsOverviewBarRenderer(list));

        // disable bar outlines...
        final BarRenderer renderer = (BarRenderer) plot.getRenderer();
        
        renderer.setItemURLGenerator(new StandardCategoryURLGenerator(""));
        
        
        
        
        
        
        
        
        return new ChartPanel(chart);
     
     
     }
     
     
     private static XYSeries getSeriesFloat(ArrayList<Float> values, String name)
     {
        XYSeries series = new XYSeries(name);
        Iterator<Float> it = values.iterator();
        series.add(0,0 );
        int counter = 1;
        while(it.hasNext())
        {
                Double value = new Double(it.next());
                series.add(counter, value.doubleValue() );
                counter ++;

        }
        return series;
        
     
     }
     
     
     
     
      private static XYSeries getSeriesDouble(ArrayList<Double> values, String name) //hex hex
     {
        XYSeries series = new XYSeries(name);
        Iterator<Double> it = values.iterator();
        series.add(0,0 );
        int counter = 1;
        while(it.hasNext())
        {
                Double value = new Double(it.next());
                series.add(counter, value.doubleValue() );
                counter ++;

        }
        return series;
        
     
     }
     
     public static ChartPanel getPieChart(Project project) 
     {
        final DefaultPieDataset dataset = new DefaultPieDataset();
        int week = project.getCurrentWeek()-1;
        float bt, re, pm, ad, ii;
        float totalWorked = 100/project.getTotalWorked();
        
        bt = project.getEvolutionalWorkedBT().get(week) * totalWorked;
        re = project.getEvolutionalWorkedRE().get(week) * totalWorked;
        pm = project.getEvolutionalWorkedPM().get(week) * totalWorked;
        ii = project.getEvolutionalWorkedII().get(week) * totalWorked;
        ad = project.getEvolutionalWorkedAD().get(week) * totalWorked;
        
        
        
        
        
        dataset.setValue("BT (" + bt + "%)", new Double(bt));
        dataset.setValue("RE (" + re + "%)", new Double(re));
        dataset.setValue("PM (" + pm + "%)", new Double(pm));
        dataset.setValue("AD (" + ad + "%)", new Double(ad));
        dataset.setValue("II (" + ii + "%)", new Double(ii));
        
        
        final JFreeChart chart = ChartFactory.createPieChart3D(
            "Zeitverteilung der Arbeitsbereiche",  // chart title
            dataset,                // data
            true,                   // include legend
            true,
            false
        );

        final PiePlot3D plot = (PiePlot3D) chart.getPlot();
        plot.setStartAngle(290);
        plot.setDirection(Rotation.CLOCKWISE);
        plot.setForegroundAlpha(0.5f);
        plot.setNoDataMessage("No data to display");
        return new ChartPanel(chart);
        
     
     
     }
      
      
      
      
      public static ChartPanel getDetailWorkedChart(Project project) 
    {
    	XYSeriesCollection dataset = new XYSeriesCollection();
        
            //float value = project.getWorkedPercent();
            String name = project.getName();
            
        
        
		// row keys...
		//String series1 = "Prozent";

		// column keys...
		//String category1 = "ALPHA";


		ArrayList<Float> workedHoursPerWeekAD = project.getEvolutionalWorkedAD();
                ArrayList<Float> workedHoursPerWeekBT = project.getEvolutionalWorkedBT();
                ArrayList<Float> workedHoursPerWeekII = project.getEvolutionalWorkedII();
                ArrayList<Float> workedHoursPerWeekPM = project.getEvolutionalWorkedPM();
                ArrayList<Float> workedHoursPerWeekRE = project.getEvolutionalWorkedRE();

                
                dataset.addSeries(getSeriesFloat(workedHoursPerWeekAD,"Worked AD"));
                dataset.addSeries(getSeriesFloat(workedHoursPerWeekBT,"Worked BT"));
                dataset.addSeries(getSeriesFloat(workedHoursPerWeekII,"Worked II"));
                dataset.addSeries(getSeriesFloat(workedHoursPerWeekPM,"Worked PM"));
                dataset.addSeries(getSeriesFloat(workedHoursPerWeekRE,"Worked RE"));
                
                
                JFreeChart chart = createLineChart(dataset, "Projektverlauf Details " + project.getName(), "Stunden");
                
                
                
                
                
                
		return new ChartPanel(chart);
        
    }
}



